package com.yjhh.chdataservice.domain;

import com.yjhh.chdataservice.util.DataConsts;
import com.yjhh.chdataservice.util.IdGenerator;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.annotation.Transient;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Class description:所有实体类的基础父类
 */
@Data
@MappedSuperclass
public class BaseEntity {

    /** Field description：主键标识 */
    @Id
    @Column(
        name       = "id",
        unique     = true,
        nullable   = false,
        insertable = true,
        length     = 64
    )
    private String id;

    /** Field description：数据状态（//1：正常;0：状态无效;-1：删除） */
    @Column(
        name   = "data_flag",
        length = 5
    )
    private Integer dataFlag;

    /** Field description ：描述 */
    @Column(
        name   = "description",
        length = 128
    )
    private String description;

    /** Field description ：创建人ID */
    @Column(
        name   = "creater_id",
        length = 64
    )
    private String createrId;

    /** Field description ：创建人名称 */
    @Column(
            name   = "creater_name",
            length = 64
    )
    private String createrName;


    /** Field description ：创建时间 */
    @Column(
        name     = "create_time",
        nullable = false,
        columnDefinition="Timestamp"
    )
    private Date createTime;

    /** Field description ：修改人ID */
    @Column(
        name   = "modify_id",
        length = 64
    )
    private String modifyId;


    /** Field description ：修改人名称 */
    @Column(
            name   = "modify_name",
            length = 64
    )
    private String modifyName;


    /** Field description： 修改时间 */
    @Column(
        name     = "modify_time",
        nullable = false,
        columnDefinition="Timestamp"
    )
    private Date modifyTime;

    @PrePersist
    public void setInsertBefore() {
        // 插入数据前设置创建时间、修改时间和数据状态
        if(StringUtils.isEmpty(this.getId())){
            this.setId(IdGenerator.createNewId());
        }
        Date dateTime = new Date();
        this.setCreateTime(dateTime);
        this.setModifyTime(dateTime);
        this.setDataFlag(DataConsts.DataStatus.DATA_FLAG_OK);
    }

    /**
     * Method description：修改前自动设置修改时间
     * Version:V1.0
     * Author:Shaok Lei
     * Date:2016.02.26 at 05:36:11 CST
     */
    @PreUpdate
    public void setUpdateBefore() {
        this.modifyTime = new Date();
    }

}
